package ro.one.gaeql.model;

import java.io.Serializable;
import java.util.Map;

public class Property implements Serializable, Evaluable {
	private static final long serialVersionUID = 1L;
	
	String alias;
	String name;
	
	public Property(String alias, String name) {
		this.alias = alias;
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	@Override
	public Object eval(EvalContext context) {
		Kind kind = Kind.getKindByAlias(this.alias, context.getAliases(), null);
		
		if(context.getEntities().containsKey(kind)) {
			Map<String, Object> entities = context.getEntities().get(kind);
			if(entities.containsKey(this.getName())) {
				return entities.get(this.getName());
			} else {
				throw new RuntimeException("Cannot get property '" + this.getName() +"' for the given entity.");
			}
		} else {
			throw new RuntimeException("Cannot get property '" + this.getName() + "' because the entity is not present in the context.");
		}
	}

	public String getAlias() {
		return alias;
	}
}
